import axios from "axios";
import { Message } from "element-ui";
import store from "../store";
import { getCookie, Token } from "@/utils/cookie";
import router from "../router/index";

// 后端联调接口
// 模拟接口-外网
const mockUrls = "http://wisdomorg.oicp.net:8083/mock/11";
// const url = process.env.NODE_ENV === 'development' ? mockUrls : process.env.LOCAL_BASE_API
// const url = process.env.NODE_ENV === 'development' ? '/api' : 'http://wisdomorg.oicp.net:8899'
// const url = mockUrls
const url = "/api";
// const url = 'http://172.21.1.5:8081' //内网
// const url = 'http://222.180.59.158:81' //外网
// const url = 'http://192.168.1.200:8080' //打包地址3
// const url = 'http://192.168.1.121:9802' //打包地址
// const url = 'http://172.19.34.40:8081' //外理内网
// const url = 'http://106.14.116.182:8081' //外理外网
// const url = 'http://47.107.228.233:8089/wzjzx' //测试
// const url = 'http://47.107.228.233:8089' //测试
// const url = 'http://192.168.1.121:8180' //外理

// 创建axios实例
const service = axios.create({
    baseURL: url, // 开发环境跨域配置的地址
    timeout: 30000 // 请求超时时间
});


const loadingOver = () => {
    if (store.getters.loadingBtn) {
        store.commit("SET_LOADING_STATUS", false);
    }
};

// request拦截器
service.interceptors.request.use(
    config => {
        // 请求头统一配置
        // config.headers["Content-Type"] = "application/json";
        config.headers["Accept"] = "application/json;charset=UTF-8";
        if (getCookie(Token)) {
            config.headers["Authorization"] = getCookie(Token); // 请求头携带Authorization为Token值
        }
        if (config.method === "put" || config.method === "post") {
            store.commit("SET_LOADING_STATUS", true);
        }
        return config;
    },
    error => {
        loadingOver();
        Promise.reject(error);
    }
);

// response拦截器
service.interceptors.response.use(
    response => {
        loadingOver();
        return response.data;
    },
    error => {
        loadingOver();
        //console.log(error.response)

        // let message = error.response.data.msg
        let message =
            error.response.data.msg || error.response.data.error || false;
        let msg = "";
        if (message) {
            if(message.indexOf("(系统找不到指定的文件。)") > 0){

            }else{
                msg =
                    message.indexOf("错误信息:") > 0
                        ? message.split("错误信息:")[1]
                        : message;
                Message({
                    message: `${msg}`,
                    showClose: true,
                    type: "error",
                    duration: 3 * 1000
                });
            }
        } else {
            msg = "服务器异常,请联系系统管理员";
            Message({
                message: `${msg}`,
                showClose: true,
                type: "error",
                duration: 3 * 1000
            });
        }
        if (error.response.status === 504 || error.response.status === 511||msg.indexOf('用户未登录')>0) {
            router.replace("/Login");
            return;
        }

        return Promise.reject(error);
    }
);

export default service;
